<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="renderer" content="webkit">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>还款申请</title>
<jsp:include page="../../import.jsp" flush="true" />
<style type="text/css">
	a:link{text-decoration: none; color:blue; font-family: 微软雅黑;}
	a:visited{color:green;}
	a:hover{text-decoration: underline; color:#09f;}
	a:active{text-decoration: blink; color: yellow;}
</style>
</head>
<body class="iframeBody">
	<div class="Wrapper">
		<div id="pageInTab" class="pageInTab">
			<form id="fnc_prj_pay_schmd_form">
				<div class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="caption">
						<span class="title">项目基础信息</span>
					</div>
					<div class="fieldBox col_50">
						<div class="label">项目名称</div>
						<div class="field">
	                       <!--  <span name="FNCPRJNM" id="FNCPRJNM" >${bscinfo.FNCPRJNM}</span>-->
						   <a  id="FNCPRJNM" href="javascript:detailBtn('${bscinfo.PID}')">${bscinfo.FNCPRJNM}</a>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">项目编号</div>
						<div class="field">
							<span name="FNCPRJID" id="FNCPRJID" >${bscinfo.FNCPRJID}</span>
							<input type="hidden" id="payschid" name="payschid" value="${bscinfo.SCHDID}">
							<input type="hidden" id="oper" name="oper" value="${oper}">
							<input type="hidden" id="jrnlid" name="jrnlid" value="${jrnlid}" >
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">创建日期</div>
						<div class="field">
							<span name="CRTTM" id="CRTTM" >${bscinfo.CRTTM}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">融资机构</div>
						<div class="field">
							<span name="FUNDRASINSNM" id="FUNDRASINSNM" >${bscinfo.FUNDRASINSNM}</span>
							<input type="hidden" id="fundrasinsid" name="fundrasinsid" value="${bscinfo.FUNDRASINSID}">
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">融资方案</div>
						<div class="field">
							<!--  <span name="FNCSCMNM" id="FNCSCMNM" >${bscinfo.FNCSCMNM}</span>-->
							<a href="javascript:showDetail('${bscinfo.FNCSCMID}')" >${bscinfo.FNCSCMNM}</a>
							<input type="hidden" id="fncscmid" name="fncscmid" value="${bscinfo.FNCSCMID}">
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">项目金额</div>
						<div class="field">
							<span name="FNCPRJAMT" id="FNCPRJAMT" >${bscinfo.FNCPRJAMT}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">实际融资金额</div>
						<div class="field">
							<span name="FNCFIAMT" id="FNCFIAMT" >${bscinfo.ACFIAMT}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">支付表数量</div>
						<div class="field">
							<span name="PAYCNT" id="PAYCNT" >${bscinfo.PAYCNT}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">方案XIRR(%)</div>
						<div class="field">
							<span name="FNCFIAMT" id="FNCFIAMT" >${bscinfo.IRR*100}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">实际XIRR(%)</div>
						<div class="field">
							<span name="PAYCNT" id="PAYCNT" >${bscinfo.ACXIRR*100}</span>
						</div>
					</div>
				</div>
				<div class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="caption">
						<span class="title">账户信息</span>
					</div>
					<div class="fieldBox col_50">
						<div class="label">机构收款账户</div>
						<div class="field">
							<!-- <span name="raccno" id="raccno" ></span> -->
							<a href='#' onclick='showInsAcc(${bscinfo.RACCID})'> <font color='blue'>${raccno}</font></a>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">我司付款账户</div>
						<div class="field">
							<!-- <span name="paccno" id="paccno" ></span> -->
							<a href='#' onclick='showSysAcc(${bscinfo.PACCID})'> <font color='blue'>${paccno}</font></a>
						</div>
					</div>
				</div>	
				<div class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="caption">
						<span class="title">融资端费用</span>
					</div>
					<div class="fieldBox col_100">
							<div id="cost0" class="sq-grid">
							</div>
					</div>
				</div>
				<div class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="caption">
						<span class="title">还款明细</span>
					</div>
					<div class="fieldBox col_100">
							<div id="cost1" class="sq-grid">
							</div>
					</div>
				</div>
				<div class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="fieldBox col_100">
						<div class="label">还款本金合计</div>
						<div class="field">
							<input name="sumFeeAmt" id="sumFeeAmt" type="text" class="ui-textbox" disabled="disabled">
						</div>
					</div>
				</div>
			</form>
		</div>
		<c:if test="${oper =='synview'}">
		<div class="btnsBox" id="ss" align="center">
			<span id="repay_list_add_btn_ok" class="sq-button sq-button-round" onclick="checkRepay()">确认无误</span>
		</div>
		</c:if>
		<c:if test="${oper =='synview2'}">
		<div class="btnsBox" id="ss" align="center">
			 <span id="repay_list_add_btn_id" class="sq-button sq-button-round" >同步资金系统</span> 	
		</div>
		</c:if>
	</div>
<!-- 嵌入账户详情 -->
<jsp:include page="../mrgnmanageinfo/showAcc.jsp"></jsp:include>
	<script type="text/javascript">
	var griddata0,griddata1;
	$(function () {
		initCost0Grid();
		initDetlGrid();
    	saveFncPrjPayEvent();
	});

    //保存事件
    function saveFncPrjPayEvent(){
        	$("#repay_list_add_btn_id").click(function(event) {
        		synFundSys();
        	});
    };
    
    function synFundSys(){
    	var jrnlid = $("#jrnlid").val() ;
		if(!jrnlid)return ;

		var jsondata = '{jrnlid:'+jrnlid+'}' ;
    	$.ajax({
      	   type: "POST",
     	   url: "<%=path%>/rfbase/fncprjpaysch/synFundSys.do",
     	   contentType: 'application/json',
     	   data: JSON.stringify(jsondata),
     	   dataType:"json",
       		success:function(msg){
    			 if(msg.msgcode == "0000"){
  					alert("同步成功!");
  					griddata0.reload();
  					griddata1.reload();
       			 }else{
      				UIUtils.alertErrorHold1(msg.errormsg);
       			 }
    		}
     	});
    }
    
    function initCost0Grid()
    {
    	var jrnlid = $("#jrnlid").val() ;
    	griddata0 = $("#cost0").ligerGrid({
	    	url : '<%=path%>/rfbase/fncprjpaysch/initCost0JrnlGrid.do?jrnlid='+jrnlid,
	    	method : 'get',
            columns: [
                { display: '费用名称', name: 'COST_NM', width: 160, type: 'text'},
                { display: '应还金额', name: 'FEE_AMT', width: 160, type: 'float'},
                { display: '已还金额', name: 'PAID_UP_AMT', width: 160, type: 'float'},
                { display: '剩余金额', name: 'AR_AMT', width: 160, type: 'float',
                    totalSummary:
                    {
                        render: function (suminf, column, cell)
                        {
                            return '<div>合计:' + suminf.sum + '</div>';
                        },
                        align: 'left'
                    }
    			},
                { display: '应还金额浮动', name: 'FLOAT_AMT',type: 'float', 
                    editor: { type: 'float' }, align: 'left', width: 160
                },
                { display: '本次还款额', name: 'ACTUAL_AMT',type: 'float', 
                    editor: { type: 'float' }, align: 'left', width: 160,
                    totalSummary:
                    {
                        render: function (suminf, column, cell)
                        {
                            return '<div>' + suminf.sum + '</div>';
                        },
                        align: 'left'
                    }
                },
                { display: '备注', name: 'FEE_REM', width: 200, type: 'text',align: 'left'
                },
                { display: '实际付款时间', name: 'ACT_PYMT_DT', width: 160, type: 'date',render:renderDate,  editor: { type: 'date' } },
                { display: '实际付款金额', name: 'ACT_PYMT_AMT', width: 160, type: 'text'},
                { display: '机构收款账号', name: 'rcvpymtAccNoHtml', width: 150, type: 'text'}, 
                { display: '我司付款账户', name: 'payAccNm', width: 200, type: 'text'},
                { display: '我司付款账号', name: 'payAccNoHtml', width: 200, type: 'text'},
                { display: '逻辑序号', name: 'ID',width:0, align:'left', hide:true}
            ],
            enabledEdit: false, isScroll: true,rownumbers:true,
            onAfterShowData: f_onAfterShowData0,
            width: '100%',height:160,checkbox:true,usePager:false
        });
    }
	
    function initDetlGrid()
    {
    	var jrnlid = $("#jrnlid").val() ;
    	griddata1 = $("#cost1").ligerGrid({
	    	url : '<%=path%>/rfbase/fncprjpaysch/initDetlJrnlGrid.do?jrnlid='+jrnlid,
	    	method : 'get',
            columns: [
            { display: '期数', name: 'payPd', width: 50, type: 'int' },
            { display: '应还款项', name: 'feeClsCd',width: 150, type: 'text',
                render: function (item)
                {
                    if (parseInt(item.feeClsCd) == 1) return '本金';
                    if (parseInt(item.feeClsCd) == 2) return '利息';
                    if (parseInt(item.feeClsCd) == 3) return '超额抵押';
                    return '其他费用';
                }},
            { display: '还款日', width: 150, name: 'payDt',render:renderDate},
            { display: '应还金额', name: 'feeAmt', width: 160, type: 'float'},
            { display: '已还金额', name: 'paidUpAmt', width: 160, type: 'float'},
            { display: '剩余金额', name: 'arAmt', width: 160, type: 'float',
                totalSummary:
                {
                    render: function (suminf, column, cell)
                    {
                        return '<div>合计:' + suminf.sum + '</div>';
                    },
                    align: 'left'
                }
			},
            { display: '应还金额浮动', name: 'floatAmt',type: 'float', 
                editor: { type: 'float' }, align: 'left', width: 160,
            },
            { display: '本次还款额', name: 'actualAmt',type: 'float', 
                editor: { type: 'float' }, align: 'left', width: 160,
                totalSummary:
                {
                    render: function (suminf, column, cell)
                    {
                        return '<div>' + suminf.sum + '</div>';
                    },
                    align: 'left'
                }
            },
            { display: '备注', name: 'feeRem', width: 200, type: 'text',align: 'left'
            },
            { display: '实际付款时间', name: 'actPymtDt', width: 160, type: 'date',render:renderDate,  editor: { type: 'date' } },
            { display: '实际付款金额', name: 'actPymtAmt', width: 160, type: 'text'},
            { display: '机构收款账号', name: 'rcvpymtAccNoHtml', width: 150, type: 'text' }, 
            { display: '我司付款账户', name: 'payAccNm', width: 200, type: 'text'},
            { display: '我司付款账号', name: 'payAccNoHtml', width: 200, type: 'text'},
            { display: '逻辑序号', name: 'id',width:0, align:'left', hide:true}
            ],
            enabledEdit: false, isScroll: true,rownumbers:true,
            onAfterShowData: f_onAfterShowData,
            width: '100%',height:350,checkbox:true,usePager:false
        });
    }

    //加载默认选中行
    function f_onAfterShowData(currentData){
    	var sumFeeAmt = 0;
    	if(currentData!=null){
	    	var arr = eval(currentData);      
	        var rows = arr.Rows;
            var manager = $("#cost1").ligerGetGridManager();
	        for(var i=0;i<rows.length;i++){
	        	if(rows[i].actualAmt!=null && rows[i].actualAmt != 0){
            		manager.select(i);
	        	}
	        	if(rows[i].feeClsCd == 1){
	        		sumFeeAmt = accAdd(sumFeeAmt , rows[i].feeAmt);
	        	}
	        }
	        $("#sumFeeAmt").val(sumFeeAmt);
    	}
    }

    //加载默认选中行
    function f_onAfterShowData0(currentData){
    	if(currentData!=null){
	    	var arr = eval(currentData);      
	        var rows = arr.Rows;
            var manager = $("#cost0").ligerGetGridManager();
	        for(var i=0;i<rows.length;i++){
	        	if(rows[i].ACTUAL_AMT!=null && rows[i].ACTUAL_AMT != 0){
            		manager.select(i);
	        	}
	        }
    	}
    }


	//加法函数，用来得到精确的加法结果
	//说明：javascript的加法结果会有误差，在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
	//调用：accAdd(arg1,arg2)
	//返回值：arg1加上arg2的精确结果
	function accAdd(arg1,arg2){
	 var r1,r2,rr,m,maxlen,result;
	 try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
	 try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}	 
	 console.log("length:"+r1+"_"+r2);
	 maxlen = Math.max(r1,r2);
	 m=Math.pow(10,maxlen);
	 console.log("Math.pow:"+m);
	 result = (arg1*m+arg2*m)/m;
	 console.log("result:"+result);
	 return result.toFixed(maxlen);
	}
	//格式化时间
	function renderDate(rowdata, index, value){
		var dateStr = "";
		if(null != value){
			var date = new Date(value);
			dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
		}
		return dateStr;
	}
	
	 //查询项目详情 
    function detailBtn(id){
		Add_navTab('tab_project_detail'+id,'项目基本信息详情','<%=path%>/rfbuss/fncprjbscinfo/toDetailPage1.do?id='+id);
	}
    //查询报价详情
	function showDetail(id){
		var callback = function(){
			 grid.reload();
		}
		var timestamp=new Date().getTime();
		Add_navTab( 'tab_scm_bsc_info_detl'+timestamp,'查看融资报价','<%=path%>/rfbase/scmbaseinfo/toDetail.do?id='+id,callback);
  	}
    //确认还款
    function checkRepay(){
    	var jrnlid = $("#jrnlid").val() ;
    	$.ligerDialog.SQopen({
            cls: 'noBorder_Dialog',
            width: 630,
            height:420,
            title : '付款项统计',
            url:'<%=path%>/rfbase/fncprjpaysch/tocheckRepay.do?id='+jrnlid,
            // 自定义buttons的时候 不要设置 fixButtons: true
            buttons: [
                 {
                    text: "保存", type: 'ok', cls: 'l-dialog-btn-ok', 
                    onclick: function (item, Dialog) {
                        //在子页面里绑定 frameElement.dialog.onOk = function () {}
                        if (Dialog.onOk) {
                        	Dialog.onOk();
                        }
                        
                    }
                }, 
                {
                    text: "取消", type: 'cancel', cls: 'l-dialog-btn-no', 
                    onclick: function (item, Dialog) {
                        //在子页面里绑定 frameElement.dialog.onCancel = function () {}
                        if (Dialog.onCancel) Dialog.onCancel();
                    }
                }
            ]
        });
    }
	</script>
</body>
</html>